"use strict";

module.exports = {
  // 在执行数据库升级时调用的函数，创建表
  up: async (queryInterface, Sequelize) => {
    const { INTEGER, DATE, STRING, ENUM } = Sequelize;
    await queryInterface.createTable("users", {
      id: {
        type: INTEGER,
        primaryKey: true,
        autoIncrement: true,
        comment: "用户唯一标识符",
      },
      account: {
        allowNull: false,
        type: STRING(30),
        comment: "用户账号，最大长度为30个字符",
        unique: true,
      },
      password: {
        allowNull: false,
        type: STRING(100),
        comment: "用户密码，最大长度为100个字符",
      },
      role_id: {
        type: INTEGER,
        comment: "用户角色id",
        references: {
          model: "role",
          key: "id",
          as: "role",
          onDelete: "CASCADE",
        },
      },
      gender: {
        type: ENUM,
        values: ["male", "female", "other"],
        allowNull: false,
        comment: "用户性别，可选值为 male, female, other",
      },
      name: {
        allowNull: false,
        type: STRING(30),
        comment: "用户姓名，最大长度为30个字符",
      },
      age: {
        type: INTEGER,
        comment: "用户年龄",
      },
      phone: {
        type: STRING(20),
        comment: "用户手机号，最大长度为11个字符",
      },
      address: {
        type: STRING(255),
        comment: "用户地址，最大长度为255个字符",
      },
      is_show: {
        type: INTEGER,
        defaultValue: 1,
        comment: "用户是否显示，1为显示，0为隐藏",
      },
      created_at: {
        type: DATE,
        comment: "记录创建时间",
      },
      updated_at: {
        type: DATE,
        comment: "记录最后更新时间",
      },
    });
  },
  // 在执行数据库降级时调用的函数，删除表
  down: async (queryInterface) => {
    await queryInterface.dropTable("users");
  },
};
